home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 101-125 / scopedisk122 / 3d / test.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  6KB  |  182 lines

  1. /* Test Demo Program - By Steve Ludtke */
  2. /*     cre : 1/3/90    mod : 3/11/90   */
  3. /*           Copyright 1990            */
  4.  
  5. /*
  6.           The 3d library and all associated software in this distribution
  7.           is Copyright 1990 by Steven J. Ludtke. You have permission to
  8.           use and/or modify this code for any purpose commercial or non-
  9.           commercial with two conditions : I must be given credit in any
  10.           distributed product's documentation, and if any part of this
  11.           package is used in any commercial product (even Shareware) one
  12.           free copy of said software must be sent to me at the following
  13.           address : Steven Ludtke, 406 Yale Cir., Glenwood Springs, CO
  14.           81601; all other royalties are waived. This Copyright notice
  15.           must accompany any distributions of any part of this package,
  16.           and in general, the package should be distributed intact, with
  17.           no modifications. This notice must not be removed from the 3d.c,
  18.           test.c, and cube.c source code in this release. Modified version
  19.           may not be distributed without permission. If the conditions
  20.           in the disclaimer are not valid in your home state, permission
  21.           to use this software is revoked.
  22. */
  23.  
  24. /*
  25. INSTRUCTIONS : This program will display shapes stored in a standard file
  26. format from any perspective. It does not use double buffered animation. The
  27. user changes his viewpoint using the keys on the numeric keypad as is shown
  28. in the code. Also, the '5' key toggles filling on and off, and 'q' exits
  29. the program. Console i/o is used, so you have to press return before
  30. anything will take effect. Run the program as follows :
  31. 'test <file.3d> <scale, usu 1>'
  32.  
  33. The 3d file format is :
  34. number of vertices (n)
  35. x0,y0,z0
  36. x1,y1,z1
  37. .
  38. .
  39. .
  40. xn,yn,zn
  41. number of line array elements (m)
  42. a0 b0
  43. a1 b1
  44. .
  45. .
  46. .
  47. am bm
  48. <EOF>
  49.  
  50. where a are vertex numbers or colors, and b is : <0> - continue shape/line,
  51. <1> - begin new line, <2> - change fill color, <3> - change outline color.
  52.  
  53. look at the color changing warnings in the docs.
  54. */
  55.  
  56. #include<stdio.h>
  57.  
  58. #define D3VDIST 256         /* distance to vanishing point */
  59. #define REZ     512         /* calc resolution must be 2^x */
  60. #define XCEN    250         /* center of bitmap X */
  61. #define YCEN    80          /* center of bitmap Y */
  62. #define XHI     600         /* x,y limits for clipping */
  63. #define YHI     165
  64. #define XLO     0
  65. #define YLO     0
  66. #define ASPECT  23/10       /* aspect ratio */
  67. #include "3d.c"
  68.  
  69. APTR IntuitionBase,GfxBase;
  70.  
  71.  
  72. struct NewWindow NW = { 0,1,600,180,1,1,0,
  73.     WINDOWDEPTH|WINDOWDRAG|GIMMEZEROZERO|SMART_REFRESH|NOCAREREFRESH,
  74.     NULL,NULL,(UBYTE *)"3D test window",NULL,NULL,0,0,0,0,WBENCHSCREEN };
  75.  
  76. long *xxx;   /* points to x vertex data */
  77. long *yyy;   /* ... */
  78. long *zzz;
  79.  
  80. LINES *line; /* points to beginning of LINES array */
  81.  
  82.  
  83. main(argc,argv)
  84. int argc;
  85. char *argv[];
  86. {
  87. VECTOR v;
  88. FILE *in;
  89. double a1,a2,a3;
  90. char c;
  91. int i,p,m,t,nl,nv,sca,f,x,z,dx,dy,dz;
  92. struct Window *win;
  93.  
  94. GfxBase=(APTR) OpenLibrary("graphics.library",0);
  95. IntuitionBase=(APTR) OpenLibrary("intuition.library",0);
  96.  
  97. f=0;
  98. if (argc!=3) { printf("test <file> <scale>\n");  exit(0); }
  99.  
  100. in=fopen(argv[1],"r");                      /* read 3d data */
  101. if (in==NULL) { printf("fnf\n"); exit(0); }
  102. sscanf(argv[2]," %d",&sca);
  103. fscanf(in," %d",&nv);
  104. xxx=(long *)malloc(nv*sizeof(long));
  105. yyy=(long *)malloc(nv*sizeof(long));
  106. zzz=(long *)malloc(nv*sizeof(long));
  107. if (xxx==NULL || yyy==NULL || zzz==NULL) { printf("Out of memory\n"); exit(0); }
  108. for (p=0; p<nv; p++) fscanf(in," %ld , %ld , %ld",&xxx[p],&yyy[p],&zzz[p]);
  109. fscanf(in," %d",&nl);
  110. line=(LINES *)malloc(nl*sizeof(LINES));
  111. if (line==NULL) { printf("Out of memory\n"); exit(0); }
  112. for (p=0; p<nl; p++) { fscanf(in," %d %d",&t,&m);
  113.     line[p].l=t;
  114.     if (m==1) line[p].nl=1; else line[p].nl=0;
  115.     if (m==2) line[p].nc=1; else line[p].nc=0;
  116.     if (m==3) line[p].nco=1; else line[p].nco=0; }
  117.  
  118. win=(struct Window *)OpenWindow(&NW);
  119.                                         /* open a window and prepare for d3surf() */
  120. Init3Ras(win->RPort,NULL);
  121. if (win==NULL) { printf("Window err\n"); exit(1); }
  122. v.x=xxx;                                /* set up VECTOR structure */
  123. v.y=yyy;
  124. v.z=zzz;
  125. v.tx=(long *) malloc(nv*sizeof(long));
  126. v.ty=(long *) malloc(nv*sizeof(long));
  127. v.tz=(long *) malloc(nv*sizeof(long));
  128.  
  129. a1=a2=a3=0;
  130. x=z=dx=dz=p=0;
  131. p=100;
  132. dy=1;
  133. SetAPen(win->RPort,1);        /* set pens, in case no color data in file */
  134. SetOPen(win->RPort,2);
  135. SetDrMd(win->RPort,JAM1);
  136.  
  137. while ((c=getchar())!='q') {              /* read from keyboard */
  138. switch(c) {
  139. case '9': a1+=PI/20; break;               /* rotates with setxfm() */
  140. case '7': a1-=PI/20; break;
  141. case '6': a2+=PI/20; break;
  142. case '4': a2-=PI/20; break;
  143. case '3': a3+=PI/20; break;
  144. case '1': a3-=PI/20; break;
  145. case '8': p+=100; break;                   /* used with setxfm & setxfm2 */
  146. case '2': p-=100; break;
  147. case '5': f^=1; break;                    /* toggle line/surf mode */
  148. case 'i': z-=100; break;                  /* used with setxfm2 */
  149. case 'm': z+=100; break;
  150. case 'j': x-=100; break;
  151. case 'k': x+=100; break;
  152. case 'w': dz-=1; break;
  153. case 'z': dz+=1; break;
  154. case 'a': dx-=1; break;
  155. case 's': dx+=1; break;
  156. case 'e': dy+=1; break;
  157. case 'x': dy-=1; break;
  158. }
  159. if (c!=10) {
  160. setxfm(a1,a2,a3,0,p,0,sca,1);             /* set new rotation matrix */
  161. /* setxfm2(dx,dy,dz,x,p,z,sca,1);         /* set new rotation matrix */
  162. /* rotatevp(&v,nv);          /*  use with setxfm2, rotate with perspective */
  163. rotatev(&v,nv);                           /* rotate with perspective */
  164. SetRast(win->RPort,0);                    /* clear window */
  165. if (f==0) d3lines(&v,line,nl,win->RPort); /* draw lines or filled shapes */
  166. else d3surf(&v,line,nl,win->RPort);
  167. }
  168. }
  169.  
  170. Exit3d(win->RPort);                       /* free up scratch memory */
  171. CloseWindow(win);
  172. free(v.tx);
  173. free(v.ty);
  174. free(v.tz);
  175. free(xxx);
  176. free(yyy);
  177. free(zzz);
  178. free(line);
  179. }
  180.  
  181.  
  182.